package com.shyb.lingxian.mine.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shyb.lingxian.R;
import com.shyb.lingxian.Util.AppUtil;
import com.shyb.lingxian.Util.StringUtils;
import com.shyb.lingxian.Util.ToastUtil;
import com.shyb.lingxian.base.BaseActivity;
import com.shyb.lingxian.mine.presenter.AddAdressPresenter;
import com.shyb.lingxian.mine.view.AddAdressView;

/**
 * Created by gs on 2018/3/26.
 */

public class AddAdressActivity extends BaseActivity<AddAdressPresenter> implements AddAdressView,View.OnClickListener,RadioGroup.OnCheckedChangeListener{

    private LinearLayout headRight;
    private RadioButton addadress_company;
    private RadioButton addadress_house;
    private RadioButton addadress_other;
    private Button addaddress_save;
    private Button addaddress_delet;
    private EditText newAddress_name;
    private EditText newAddress_phone;
    private TextView tv_address3;
    private EditText newAddress_room;
    private String type;
    private String typeId;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newaddress_add);

        initHeadTitle("新增收货地址");

        headRight = (LinearLayout) findViewById(R.id.lin_head_right);
        headRight.setOnClickListener(this);
        headRight.setVisibility(View.VISIBLE);

        findViews();
    }

    private void findViews() {
        ImageView headLeft = (ImageView) findViewById(R.id.iv_head_left);
        headLeft.setOnClickListener(this);

        intHeadRight("保存");

        addadress_company = (RadioButton) findViewById(R.id.addadress_company);     //公司
        addadress_house = (RadioButton) findViewById(R.id.addadress_house);         //家庭
        addadress_other = (RadioButton) findViewById(R.id.addadress_other);         //其他


        addaddress_save = (Button) findViewById(R.id.addaddress_save);
        addaddress_delet = (Button) findViewById(R.id.addaddress_delet);

        addaddress_save.setOnClickListener(this);
        addaddress_delet.setOnClickListener(this);

        newAddress_name = (EditText) findViewById(R.id.newAddress_name);        //名字
        newAddress_phone = (EditText) findViewById(R.id.newAddress_phone);      //电话
        newAddress_room = (EditText) findViewById(R.id.newAddress_room);        //房间号

        tv_address3 = (TextView) findViewById(R.id.tv_address3);                //地址
        tv_address3.setOnClickListener(this);


        TextView tv_head_mid = (TextView) findViewById(R.id.tv_head_mid);
        String headMsg = tv_head_mid.getText().toString();
        if (headMsg.equals("新增收货地址")){
            addaddress_delet.setText("重置");
        }else if (headMsg.equals("编辑收货地址")){
            addaddress_delet.setText("删除");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_head_left:
                onBackPressed();
                break;

            case R.id.lin_head_right:     //保存
                presenter.SaveNewAddress(this);

                break;

            case R.id.addaddress_save:     //保存button
                presenter.SaveNewAddress(this);
                break;

            case R.id.addaddress_delet:     //删除
                String deletMsg = addaddress_delet.getText().toString();
                if (deletMsg.equals("重置")){
                    newAddress_name=null;
                    newAddress_phone = null;
                    newAddress_room = null;
                    tv_address3 = null;
                    addadress_company.setChecked(true);
                }else if (deletMsg.equals("删除")){

                }

                break;

            case R.id.tv_address3:          //地址

                break;


        }
    }

    @Override
    public void onError(String message) {
        ToastUtil.showToast(this,message);
    }

    @Override
    protected AddAdressPresenter initPresenter() {
        return new AddAdressPresenter(this);
    }

    @Override
    protected void initialize() {

    }

    @Override
    public String getRealName() {

        return newAddress_name.getText().toString().trim();
    }

    @Override
    public String getRealPhone() {
        return newAddress_phone.getText().toString().trim();
    }

    @Override
    public String getRealProvice() {

        return tv_address3.getText().toString().trim();
    }

    @Override
    public String getRealAddress() {

        return newAddress_room.getText().toString().trim();
    }

    @Override
    public String getCategory() {
        return typeId;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.addadress_company:
                type = "公司";
                typeId = "1";

                break;

            case R.id.addadress_house:
                type = "住宅";
                typeId = "2";

                break;

            case R.id.addadress_other:
                type = "其他";
                typeId = "3";

                break;
        }
    }
}
